#include <iostream>

using namespace std;

//深拷贝与浅拷贝

class Person
{
public:
    Person()
    {
        cout << "Person的默认构造函数调用" << endl;
    }

    Person(int age, int height)
    {
        m_Age = age;
        m_Height = new int(height);
        cout << "Person的有参构造函数调用" << endl;
    }

    //浅拷贝带来的问题就是堆区的内存重复释放
    //自己实现拷贝构造函数 解决浅拷贝带来的问题

    Person(const Person & p)
    {
        m_Age = p.m_Age;
        m_Height = new int(*p.m_Height);
        //深拷贝操作
        
        cout << "Person的拷贝构造函数调用" << endl;
    }

    ~Person()
    {
        //析构代码，将堆区开辟数据做释放
        if(m_Height != NULL)
        {
            delete m_Height;
            m_Height = NULL;
        }
        cout << "Person的析构函数调用" << endl;
    }

    int m_Age;
    int *m_Height;
};

void test01()
{
    Person p1(18, 160);

    cout << "p1的年龄为：" << p1.m_Age << " 身高为：" << * p1.m_Height << endl;

    Person p2(p1);

    cout << "p2的年龄为：" << p2.m_Age << " 身高为：" << * p2.m_Height << endl;
}

int main(void)
{
    test01();

    return 0;
}